package com.miquan.minotehelper.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;

import com.cj.ScreenShotUtil.ScreentShotUtil;
import com.miquan.minotehelper.activity.ClipImageActivity;

import java.io.File;

public class ScreenShotService extends Service {
    private Context context;

    public ScreenShotService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        boolean isScreenShot = intent.getBooleanExtra("screen_shot", false);
        if (isScreenShot) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        screentShot();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void screentShot() {
        String fileName = "screen_shot.png";

        //获取图片存储路径
        File dir = new File(Environment.getExternalStorageDirectory(), "MINoteHelper");
        if (!dir.exists()) dir.mkdirs();
        File file = new File(dir, fileName);

        ScreentShotUtil.getInstance().takeScreenshot(context, file.getAbsolutePath());

        ClipImageActivity.startInNewTask(context, file.getAbsolutePath());
    }
}
